package xuhaijia.lanou3g.autohome.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.base.BaseActivity;

/**
 * Created by dllo on 16/7/22.
 */
public class WelcomeActivity extends BaseActivity implements View.OnClickListener {

    private ImageView welcomeIv;
    private Button welcomeBtn;
    private Count time;
    private ContentResolver resolver;
    private SimpleDateFormat mFormat;
    public  Intent play;


    @Override
    protected int setLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initView() {
        welcomeIv = (ImageView) findViewById(R.id.welcome_iv);
        welcomeBtn = (Button) findViewById(R.id.welcome_timeBtn);
        play = new Intent(WelcomeActivity.this , PlayService.class);
        startService(play);
    }

    @Override
    protected void initData() {
        welcomeBtn.setOnClickListener(this);
        WelcomeAsyncTask welcomeAsyncTask = new WelcomeAsyncTask();
        welcomeAsyncTask.execute("http://img2.cnxz.cn/photo_upload/kindeditor/201407/03/20140703034503535.jpg");
        time = new Count(7000, 1000);
        time.start();
    }


    @Override
    public void onClick(View view) {
        time.cancel();
        Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private class WelcomeAsyncTask extends AsyncTask<String, Integer, Bitmap> {
        Bitmap bitmap = null;
        HttpURLConnection connection = null;
        InputStream is = null;

        protected Bitmap doInBackground(String... strings) {
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    InputStream is = connection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(is);
                    is.close();
                    connection.disconnect();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            welcomeIv.setImageBitmap(bitmap);

        }
    }

    private class Count extends CountDownTimer {
        public Count(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long l) {
            welcomeBtn.setText(l / 1000 + "s");
        }

        @Override
        public void onFinish() {
            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }


    }

}
